﻿using NCP.Core.Exceptions;
using NCP.Model;
using NCP.Service;
using NCP.Util;
using NCP.Util.Extend;
using Newtonsoft.Json;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using WebApp.Filters;

namespace WebApp.Controllers
{
    [Authen]
    public class GroupMissionController : Controller
    {
        private GroupMissionService _service;
        private DeptMergeInfoService _deptService;
        private GroupPurchaseService _purchaseService;
        private GroupChoseDrugCompareService _CompareService;
        public GroupMissionController(GroupMissionService service, 
            DeptMergeInfoService deptService,
            GroupPurchaseService purchaseService,
            GroupChoseDrugCompareService CompareService)
        {
            _deptService = deptService;
            _service = service;
            _purchaseService = purchaseService;
            _CompareService = CompareService;
        }

        // GET: GroupMission
        public ActionResult Index()
        {
            List<GroupPurchase> list = _CompareService.GroupPurchaseSelect();
            ViewData["gplist"] = list;
            return View();
        }

        public ActionResult GetList(string gpCode,string drugCode)
        {
            Expressionable<GroupMission> exp = new Expressionable<GroupMission>();
            exp.And(t => t.GpCode.Equals(gpCode));
            exp.And(t => t.DrugCode.Equals(drugCode));
            List<GroupMission> dataList = _service.GetAllList(exp);
            LayData ht = new LayData("0", "获取成功", dataList);
            return Content(JsonConvert.SerializeObject(ht));
        }


        public ActionResult Upload()
        {
            var files = Request.Files;

            if (files.Count > 0)
            {
                var filePath = AppDomain.CurrentDomain.BaseDirectory + "\\upload\\" + files[0].FileName;
                if (files[0].ContentLength > 0)
                {
                    files[0].SaveAs(filePath);
                    DataTable dt = ExcelHelper.ExcelToDataTable(filePath);
                    List<GroupMission> itemlist = new List<GroupMission>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        string gpcode = dr.GetVal("集采编码");
                        string drugcode = dr.GetVal("药品编码");
                        string gpname = dr.GetVal("集采名称");
                        string drugname = dr.GetVal("药品名称");
                        string deptname = dr.GetVal("任务科室");
                        string deptcode = dr.GetVal("科室编码");
                        string qty = dr.GetVal("完成任务量");
                        string rate = dr.GetVal("完成比例");

                        GroupMission mission = new GroupMission();
                        mission.GpCode = gpcode;
                        mission.GpName = gpname;
                        mission.DrugCode = drugcode;
                        mission.DeptName = deptname;
                        if (!_deptService.Exists(deptcode)) //代码是否存在
                            continue;
                        mission.DeptCode = deptcode;// _deptService.GetCodeByName(deptname);
                        mission.Qyt = qty.ObjToDecimal();
                        mission.Rate = rate.ObjToDecimal();
                        itemlist.Add(mission);

                    }
                    if (itemlist.Count > 0)
                        _service.SaveMission(itemlist);
                }

                return Json("1");
            }
            else
            {
                return Json("-1");
            }
        }

        public ActionResult GetGpList()
        {
            Expressionable<VGpinfo> exp = new Expressionable<VGpinfo>();
            int pageIndex = 1;
            int pageSize = 20;
            if (!string.IsNullOrEmpty(Request.Form["page"]))
            {
                pageIndex = Convert.ToInt32(Request.Form["page"]);
            }
            if (!string.IsNullOrEmpty(Request.Form["limit"]))
            {
                pageSize = Convert.ToInt32(Request.Form["limit"]);
            }
            string gpCode = Request["GroupPurchase"];
            if (string.IsNullOrEmpty(gpCode))
            {
                List<VGpinfo> lst = new List<VGpinfo>();
                LayData rst = new LayData("0", "获取成功", lst, 0);

                return Content(JsonConvert.SerializeObject(rst));
            }
            exp.And(t => t.GpCode == gpCode);
            exp.AndIF(!string.IsNullOrEmpty(Request["drugCode"]), t => t.DrugCode == Request["drugCode"]);
            PageModel pageModel = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
            List<VGpinfo> dataList = _service.GetGpListForPage(exp, pageModel);
            LayData ht = new LayData("0", "获取成功", dataList, pageModel.TotalCount);

            return Content(JsonConvert.SerializeObject(ht));
        }

        public ActionResult MissionView(string GpCode,string DrugCode)
        {
            GroupPurchase purchase = _purchaseService.GetGroupPurchase(GpCode);
            Expressionable<VGpinfo> exp = new Expressionable<VGpinfo>();
            exp.And(t=>t.GpCode==GpCode);
            exp.And(t=>t.DrugCode==DrugCode);
            VGpinfo item = _service.GetVGpinfo(exp);
            ViewData["enable"] = purchase.Enabled;
            return View(item);
        }

        public ActionResult SaveMission(List<GroupMission> items, string gpCode,string drugCode)
        {
            LayData data = new LayData("0", "保存成功");
            try
            {
                _service.SaveMission(items, gpCode,drugCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }

        public ActionResult Export()
        {
            DataTable dt = _service.export(Request["GroupPurchase"], Request["drugCode"]);
            string filename = ExcelHelper.ExportToExcel(dt, "集采任务信息");
            return File(filename, "application/vnd.ms-excel", "集采任务信息.xls");
        }

    }
}